//
//  WKAlerView.m
//  WeKeTang
//
//  Created by ichina on 2017/8/23.
//  Copyright © 2017年 Dezhi Tiancheng Education Technology (Beijing) Co. Ltd. All rights reserved.
//

#import "WKAlerView.h"

@interface WKAlerView ()

@property (nonatomic, strong) UIView *alertView;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UILabel *tipLabel;
@property (nonatomic, strong) UIButton *btnConfirm;
@property (nonatomic, strong) UIButton *btnCancel;
@property (nonatomic, strong) UIButton *delegateBtn;

@end

@implementation WKAlerView

- (instancetype)initWithFrame:(CGRect)frame message:(NSString *)message tip:(NSString*)tip title1:(NSString *)title1 title2:(NSString *)title2{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
        self.tip = tip;
        self.message = message;
        self.title1 = title1;
        self.title2 = title2;
        [self setupUI];
        
    }
    return self;
}
- (void)setupUI{
    
    [self addSubview:self.alertView];
    
    self.alertView.transform = CGAffineTransformMakeScale(1.2, 1.2);
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration: 0.7 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0.3 options:0 animations:^{
        
        weakSelf.alertView.transform = CGAffineTransformMakeScale(1, 1);
    } completion:nil];
    
    [self.alertView addSubview:self.tipLabel];
    [self.alertView addSubview:self.messageLabel];
    [self.alertView addSubview:self.btnConfirm];
    [self.alertView addSubview:self.btnCancel];
    [self.alertView addSubview:self.delegateBtn];
   
}

- (void)btnConfirmAction:(UIButton*)sender{
    
    [sender setBackgroundColor:RGBColor(40, 188, 203)];
    [self.btnCancel setBackgroundColor:RGBColor(208, 208, 208)];
    
    if (self.confirmBlock) {
        self.confirmBlock();
    }
    
}
- (void)btnCancelAction:(UIButton*)sender{
    [sender setBackgroundColor:RGBColor(40, 188, 203)];
    [self.btnConfirm setBackgroundColor:RGBColor(208, 208, 208)];
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}

- (void)delegateBtnAction:(UIButton*)sender{
        
    if (self.delegateBtn) {
        self.deleteBlock();
    }
}
- (void)layoutSubviews{
    
    [super layoutSubviews];
    if (_isPlayer) {
        self.tipLabel.font = [UIFont systemFontOfSize:15];
        self.tipLabel.height = 42;
        self.tipLabel.numberOfLines = 0;
    }
}
- (UIView *)alertView{
    
    if (!_alertView) {
        _alertView = [[UIView alloc]initWithFrame:CGRectMake(30, kDeviceScreenHeight * 0.5 - 90, kDeviceScreenWidth - 60, 160)];
        _alertView.layer.cornerRadius = 3;
        _alertView.layer.masksToBounds = YES;
        _alertView.backgroundColor = [UIColor whiteColor];
    }
    return _alertView;
}

- (UILabel *)tipLabel{
    
    if (!_tipLabel) {
        _tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 35, self.alertView.width, 21)];
        if (_isPlayer) {
            _tipLabel.font = [UIFont systemFontOfSize:13];
            _tipLabel.height = 42;
        }else{
            _tipLabel.font = [UIFont systemFontOfSize:15];
        }
        _tipLabel.text = self.message;
        _tipLabel.numberOfLines = 0;
        _tipLabel.textAlignment = NSTextAlignmentCenter;
        _tipLabel.textColor = RGBColor(24, 24, 24);
    }
    return _tipLabel;
}
- (UILabel *)messageLabel{
    
    if (!_messageLabel) {
        _messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.tipLabel.frame)+5, self.alertView.width, 21)];
        _messageLabel.font = [UIFont systemFontOfSize:15];
        _messageLabel.text = self.tip;
        _messageLabel.numberOfLines = 0;
        _messageLabel.textAlignment = NSTextAlignmentCenter;
        _messageLabel.textColor = RGBColor(24, 24, 24);
    }
    return _messageLabel;
}
- (UIButton *)btnConfirm{
    
    if (!_btnConfirm) {
        _btnConfirm = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnConfirm.frame = CGRectMake(40, self.alertView.height -60, (self.alertView.width -20-80)*0.5, 30);
        _btnConfirm.titleLabel.font = [UIFont systemFontOfSize:15];
        [_btnConfirm setTitle:_title1 forState:UIControlStateNormal];
        [_btnConfirm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnConfirm setBackgroundColor:RGBColor(208, 208, 208)];
        _btnConfirm.layer.cornerRadius = 2;
        _btnConfirm.layer.masksToBounds = YES;
        [_btnConfirm addTarget:self action:@selector(btnConfirmAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnConfirm;
}
- (UIButton *)btnCancel{
    
    if (!_btnCancel) {
        _btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnCancel.frame = CGRectMake(CGRectGetMaxX(self.btnConfirm.frame)+20, self.alertView.height -60, (self.alertView.width -20-80)*0.5, 30);
        _btnCancel.titleLabel.font = [UIFont systemFontOfSize:15];
        [_btnCancel setTitle:_title2 forState:UIControlStateNormal];
        [_btnCancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnCancel setBackgroundColor:RGBColor(0, 167, 185)];
        _btnCancel.layer.cornerRadius = 2;
        _btnCancel.layer.masksToBounds = YES;
        [_btnCancel addTarget:self action:@selector(btnCancelAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnCancel;
}
- (UIButton *)delegateBtn{
    
    if (!_delegateBtn) {
        _delegateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _delegateBtn.frame = CGRectMake(self.alertView.width - 15-7.5, 15, 7.5, 7.5);
        _delegateBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_delegateBtn setImage:IMG(@"closeA") forState:UIControlStateNormal];
        [_delegateBtn addTarget:self action:@selector(delegateBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _delegateBtn;
}
@end
